package cn.edu.scau.dropship.controller;

import cn.edu.scau.dropship.entity.User;
import cn.edu.scau.dropship.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    UserService userService;

    @RequestMapping("/userRegister")
    @ResponseBody
    public String register(HttpServletRequest request, HttpServletResponse response,
                           HttpSession session, Model model){
        String name = request.getParameter("name");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String password = request.getParameter("password");
        String account_type = request.getParameter("account_type");
        System.out.println("name" +":" + name +"," + "password" + ":" + password + "," + "email" +":" + email);
        User user = new User();
        user.setName(name);
        user.setPassword(password);
        user.setEmail(email);
        user.setAccountType(account_type);
        user.setPhone(phone);
        userService.insert(user);
        return "success";
    }
    @RequestMapping("/fail")
    public String fail(){
        return "fail";
    }
    @RequestMapping("/checkName")
    @ResponseBody
    public String checkName(HttpServletRequest request, HttpServletResponse response,
                            HttpSession session, Model model){
            String name = request.getParameter("check_username");
            User user = userService.checkName(name);
            if(user != null){
                return "该用户已被注册!";
            }else{
                return "该用户可注册!";
            }

    }
}
